﻿using System.Drawing;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;

namespace PlanningGIS.Framework
{
  
    public class BaseDrawRectAtForm : BaseDrawRectTool
    {
        /// <summary>
        /// 
        /// </summary>
        public AfterButtonOk afterButtonOk1;
        /// <summary>
        /// 
        /// </summary>
        public AfterButtonOkShift afterButtonOkShift1;
        /// <summary>
        /// 
        /// </summary>
        private IActiveView m_pActiveView;
        /// <summary>
        /// 
        /// </summary>
        private IMap m_pMap;

        /// <summary>
        /// 
        /// </summary>
        public BaseDrawRectAtForm()
        {
            m_cursor = Cursors.Cross;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="button"></param>
        /// <param name="shift"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public override void OnMouseUp(int button, int shift, int x, int y)
        {
            base.OnMouseUp(button, shift, x, y);
            if (button != 1)
            {
                return;
            }
            //在MapControl上画出画的多边形
            if (m_hookHelper != null)
            {
                m_pMap = m_hookHelper.FocusMap;
                m_pActiveView = m_pMap as IActiveView;
                if (m_geom != null && !(m_geom is IPoint))
                    DrawGeometryOnScreen(m_geom as IEnvelope, m_pActiveView);
            }
            if (afterButtonOk1 != null)
            {
                afterButtonOk1.Invoke(m_geom);
            }
            if (afterButtonOkShift1 != null)
            {
                afterButtonOkShift1.Invoke(m_geom, shift);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="pGeom"></param>
        /// <param name="pActiveView"></param>
        public static void DrawGeometryOnScreen(IGeometry pGeom, IActiveView pActiveView)
        {
            IRgbColor pColor = new RgbColorClass();
            pColor.Red = 255;
            pColor.Green = 255;
            pColor.Blue = 0;
            pColor.Transparency = 10;

            ISimpleLineSymbol pLineSymbol = new SimpleLineSymbolClass();
            pLineSymbol.Color = pColor;

            IDotDensityFillSymbol dfillSymbol = new DotDensityFillSymbolClass();
            dfillSymbol.Color = pColor;
            dfillSymbol.DotSize = 5;
            dfillSymbol.DotSpacing = 15;
            dfillSymbol.Outline = pLineSymbol;

            IFillSymbol pFillSymbol = new SimpleFillSymbolClass();
            pFillSymbol.Outline = pLineSymbol;
            pFillSymbol.Outline.Width = 5;
            pFillSymbol.Color = pColor;
            pFillSymbol.Color.Transparency = 100;//透明填充

            IScreenDisplay pScreenDisplay = pActiveView.ScreenDisplay;
            pScreenDisplay.StartDrawing(pScreenDisplay.hDC, (short)esriScreenCache.esriNoScreenCache);
            switch (pGeom.GeometryType)
            {
                case esriGeometryType.esriGeometryPoint:
                    IMarkerSymbol pMarkerSymbol = new SimpleMarkerSymbolClass();
                    pMarkerSymbol.Color = pColor;
                    pMarkerSymbol.Size = 4;
                    pScreenDisplay.SetSymbol(pMarkerSymbol as ISymbol);
                    pScreenDisplay.DrawPoint(pGeom);
                    break;
                case esriGeometryType.esriGeometryPolyline:
                    pScreenDisplay.SetSymbol(pLineSymbol as ISymbol);
                    pScreenDisplay.DrawPolyline(pGeom);
                    break;
                case esriGeometryType.esriGeometryPolygon:
                    pScreenDisplay.SetSymbol(pFillSymbol as ISymbol);
                    dfillSymbol.BackgroundColor = new RgbColorClass();
                    pScreenDisplay.DrawPolygon(pGeom);
                    break;
                case esriGeometryType.esriGeometryEnvelope:
                    pScreenDisplay.SetSymbol(pFillSymbol as ISymbol);
                    pScreenDisplay.DrawRectangle(pGeom as IEnvelope);
                    break;
            }
            pScreenDisplay.FinishDrawing();
        }
    }


}